package edu.lingnan.librarymanage.controller;

import edu.lingnan.librarymanage.core.base.AjaxResult;
import edu.lingnan.librarymanage.core.dto.CategoryDTO;
import edu.lingnan.librarymanage.core.vo.CategoryVO;
import edu.lingnan.librarymanage.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 * @author cql
 */
@Controller
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    @ResponseBody
    @PostMapping("/save")
    public AjaxResult insertCategory(@RequestBody CategoryDTO category) {
        CategoryVO categoryVO = categoryService.insertCategory(category);
        if (categoryVO == null) {
            return AjaxResult.error("分类名称重复");
        }
        return AjaxResult.success(categoryVO);
    }

    @ResponseBody
    @PostMapping("/update")
    public AjaxResult updateCategory(@RequestBody CategoryDTO category) {
        categoryService.updateCategory(category);
        return AjaxResult.success();
    }


    @ResponseBody
    @PostMapping("/delete/{categoryId}")
    public AjaxResult delateCategory(@PathVariable("categoryId") Integer categoryId) {
        boolean flag = categoryService.delateCategory(categoryId);
        if (!flag) {
            return AjaxResult.error("删除失败,该分类下有许多子分类!");
        }
        return AjaxResult.success();
    }

}
